<?php

function club_get($id) {
	$sql = 'SELECT * FROM `club` WHERE ID='.$id.';';
	return query($sql);
}

function club_get_list() {
	$sql = 'SELECT * FROM club;';
	return query($sql);
}

function get_palmares_club($id) {
	$sql = 'SELECT nom, palm_champ, palm_europe FROM club WHERE ID='.$id.';';
	return query($sql);
	}
	
function club_get_classement() {

        $sql = 'SELECT (nb_pts_marques-nb_pts_encaisses) AS ptavg, nom, nb_egalites, nb_matchs_perdus,nb_matchs_gagnes, nb_pts,
						nb_pts_marques, nb_matchs, nb_pts_encaisses, palm_champ, palm_europe
                FROM club ORDER BY classement ASC';
        
        return query($sql);
}

function club_maj_classement() {

        $sql = 'SELECT ID,(nb_pts_marques-nb_pts_encaisses) AS ptavg
                FROM club ORDER BY nb_pts DESC, ptavg DESC, nb_pts_marques DESC, nb_matchs_gagnes DESC, nom ASC';
        $result=query($sql);
		for($i=1 ; $club = mysql_fetch_assoc($result) ; ++$i) {
		$sql_q = 'UPDATE `club` SET `classement` =  '.$i.' WHERE ID='.$club['ID'];
		query($sql_q);
		}
		
}

function club_list_nom () {
	$sql = 'SELECT nom,ID FROM `club` ;';
	return query ($sql);
}

function update_pts($id_equipe,$points,$pm,$pe,$gnp){
	$sql = 'UPDATE `club` SET nb_pts =nb_pts+'.$points.', 
							  nb_pts_marques=nb_pts_marques+'.$pm.', 
							  nb_pts_encaisses=nb_pts_encaisses+'.$pe.',
							  nb_matchs=nb_matchs+1,';
	if ($gnp==0){
		$sql .= 'nb_matchs_gagnes=nb_matchs_gagnes+1';
	}else
		if ($gnp==2){
			$sql .= 'nb_matchs_perdus=nb_matchs_perdus+1';
		}
		else {
			$sql .= 'nb_egalites=nb_egalites+1';
		}
	$sql .= ' WHERE ID ='.$id_equipe.';';
	return query ($sql);
}

function raz_score_equipe(){
	$sql = 'UPDATE `club` SET nb_pts =0, nb_matchs=0, nb_matchs_gagnes=0, nb_egalites=0, nb_matchs_perdus=0, classement=0, nb_pts_marques=0, nb_pts_encaisses=0;';
	return query ($sql);
}

function ajouter_club($nom, $lieu, $site, $pamlmares_f, $palmares_e){
	$sql = "INSERT INTO club VALUES ('', '$nom', '0', '0', '0','0', '0', '0', '0', '0', '$lieu', '$pamlmares_f', '$palmares_e', '', '$site');";
	return query ($sql);
}

function supprimer_club ($nom_club){
	$sql = "DELETE FROM club WHERE nom = '$nom_club';";
	return query ($sql);
}

function supprimer_photo_club($nom_club){
	$sql = "UPDATE `club` SET photo = '' WHERE nom ='$nom_club';";
	return query ($sql);
}